package com.sitanyun.merchant.guide.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.DeleteBean;
import com.sitanyun.merchant.guide.bean.HistoryTodayBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.RatingBar;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by jzl
 * on 2022/1/11.
 * 用户适配器
 */
public class HistoryUserAdapter extends BaseQuickAdapter<HistoryTodayBean.DataBean, BaseViewHolder> {

    public HistoryUserAdapter(Context context) {
        super(R.layout.users_item);
        this.mContext = context;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void convert(BaseViewHolder helper, HistoryTodayBean.DataBean item) {

        if (item.getCName()==null){
            helper.setText(R.id.username,"访客");
        }else {
            helper.setText(R.id.username,item.getCName());
        }
    com.sitanyun.merchant.guide.weiht.RatingBar    ratingBar=(com.sitanyun.merchant.guide.weiht.RatingBar) helper.getView(R.id.rb);
        ratingBar.setStar(item.getStars());
    //  点击星星设置星标
        ratingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {

                HashMap<String, String> hashMap = new HashMap<String, String>();
                int b = Math.round(ratingCount);//采用round方式转换为整型
                hashMap.put("staffId", SharedPreferenceUtil.getStringData("staffid"));
                hashMap.put("consumerId", item.getCId());
                hashMap.put("stars", b+"");
                OkHttpUtils.postString()
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                        .url(Urls.setConsumerStar)
                        .content(new Gson().toJson(hashMap))
                        .build().execute(new StringCallbacks() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        DeleteBean baseEntity = new Gson().fromJson(response, DeleteBean.class);
                        if (baseEntity.getCode() == 0) {
                           ToastUtil.showToast(mContext,"设置成功");
                        }
                    }
                });

            }
        });

        ImageView   userimgs= helper.getView(R.id.userimg);
        TextView  action=helper.getView(R.id.action);
        //留言  回电
        TextView  readmsg=helper.getView(R.id.img_readmessage);
        TextView  readcall=helper.getView(R.id.img_readcall);
        if (item.isHaveMessage()==true){
            readmsg.setVisibility(View.VISIBLE);
        }else {
            readmsg.setVisibility(View.GONE);
        }
        if (item.isHaveCall()==true){
            readcall.setVisibility(View.VISIBLE);
        }else {
            readcall.setVisibility(View.GONE);
        }
        Glide.with(mContext).load(item.getHeadUrl()).error(R.mipmap.cimg).into(userimgs);
        String dateStr = item.getLastBehaviorTime();
            helper.setText(R.id.time,dateStr.substring(0,10)+"\n"+dateStr.substring(10,16));
        helper.addOnClickListener(R.id.usertraje);
        helper.addOnClickListener(R.id.interactive);
        helper.addOnClickListener(R.id.callphone);
    // 时间时分秒转换
        int intTime=item.getDuring();
        if (intTime==0){
            helper.getView(R.id.online).setVisibility(View.INVISIBLE);
        }else {
            helper.getView(R.id.online).setVisibility(View.VISIBLE);
            int h=intTime/3600;
            int m=(intTime%3600)/60;
            int s=(intTime%3600)%60;
            if (h==0){
                helper.setText(R.id.online,"已浏览"+m+"分"+s+"秒");
            }else {
                helper.setText(R.id.online,"已浏览"+h+"时"+m+"分");
            }
        }


     if (item.getOnline()==1){
         SimpleDateFormat fmtrq = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
         Date now = new Date();
         String value = item.getLastBehaviorTime();
         Date robottime = null;
         try {
             robottime = fmtrq.parse(value);
         } catch (ParseException e) {
             e.printStackTrace();
         }
         if (now.getTime() - robottime.getTime() >= 60000) {//1000表示1秒
             action.setText("已退出场景");
             action.setTextColor(Color.parseColor("#999999"));

         }else {
             action.setText("正在浏览中");
             action.setTextColor(Color.parseColor("#fff78139"));
         }
//         if (item.getDuring()<60){
//               action.setText("已退出场景");
//               action.setTextColor(Color.parseColor("#999999"));
//           }else {
//               action.setText("正在浏览中");
//               action.setTextColor(Color.parseColor("#fff78139"));
//           }
     }else {
         action.setText("已离线");
         action.setTextColor(Color.parseColor("#fff78139"));
     }



    }

}